package cn.hrc.controller;

import cn.hrc.dao.CouDao;
import cn.hrc.dao.MarkDao;
import cn.hrc.dao.TaskDao;
import cn.hrc.dao.UserDao;
import cn.hrc.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tea")
public class TeaController {
    @Autowired
    CouDao couDao;
    @Autowired
    UserDao userDao;
    @Autowired
    TaskDao taskDao;
    @Autowired
    MarkDao markDao;
    /** 去更改通过保存的session.uid进行查询
     * 【不在页面直接使用session.who.*】 */
    @RequestMapping("/go2updateMe")
    public String go2updateMe(HttpSession session, Model model){
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        User user = userDao.findByUid(who.getUid());
        model.addAttribute("user",user);
        return "tea/myUpdate";
    }
    /** 教师更改密码【自动退出登录】 */
    @PostMapping("updateMy")
    public String updateMe(HttpSession session, Model model, User user){
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        int num = userDao.updateUser(user);
        System.out.println("更改成功");//更改成功退出登录，返回登录销毁方法
        return "forward:/home/logout";
    }
    /**当前登录的教师的课程 */
    @RequestMapping("/findMyTask")
    public String findMyTask(HttpSession session, Model model){
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        List<TaskInfo> taskInfos = taskDao.findByTid(who.getUid());
        model.addAttribute("taskInfos",taskInfos);
        return "tea/myTask";
    }
    /** 进行教师批改学生成绩 */
    @PostMapping("/findMark")
    public String findMark(String clzno,String cno,HttpSession session, Model model){
        System.out.println("clzno:\t"+clzno+"\tcno:\t"+cno);
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        Map<String,Object> map = new HashMap<>();
        map.put("clzno",clzno);
        map.put("cno",cno);
        map.put("tid",who.getUid());
        System.out.println("Map:\t"+map);
        List<MarkInfo> markInfos = markDao.findMyMarkByClznoCno(map);
        System.out.println("课程成绩录入：\t"+markInfos);
        model.addAttribute("markInfos",markInfos);
        return "tea/myMark";
    }
    /** 课程成绩更改，成绩添加 */
    @PostMapping("addMark")
    public String addMark(Mark mark,String clzno,HttpSession session, Model model){
        System.out.println("成绩录入:\t"+mark);
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        if (mark.getId() > 0){
            int num = markDao.updateById(mark);
            System.out.println("更改成功：\t"+num);
        }else {
            mark.setTid(who.getUid());
            mark.setTpost(new Date());
            int num = markDao.addMark(mark);
            System.out.println("添加成功：\t"+num);
        }
        System.out.println("成绩录入\tclzno:\t"+clzno+"\tcno:\t"+mark.getCno());
        return "forward:/tea/findMark";
    }
    /**当前登录的教师的课程【Pro版】 */
    @RequestMapping("/findMyTaskPro")
    public String findMyTaskPro(HttpSession session, Model model){
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        List<TaskInfo> taskInfos = taskDao.findByTid(who.getUid());
        model.addAttribute("taskInfoPro",taskInfos);
        return "tea/myTaskPro";
    }
    /** 进行教师批改学生成绩【Pro版】 */
    @PostMapping("/findMarkPro")
    public String findMarkPro(String clzno,String cno,HttpSession session, Model model){
        System.out.println("clzno:\t"+clzno+"\tcno:\t"+cno);
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        Map<String,Object> map = new HashMap<>();
        map.put("clzno",clzno);
        map.put("cno",cno);
        map.put("tid",who.getUid());

        System.out.println("Map:\t"+map);
        /** 登分Pro版查询 */
        List<MarkInfo> markInfoPro = markDao.findMyMarkByMap(map);
        /** Pro版 循环查询成绩是否已批改 */
        for(MarkInfo markInfo:markInfoPro){
            Map<String,Object> mapPro = new HashMap<>();
            mapPro.put("tid",who.getUid());
            mapPro.put("cno",markInfo.getCourse().getCno());
            mapPro.put("sno",markInfo.getUser().getPhone());
            Mark markPro = markDao.findByMap(mapPro);
            /** 查询对应的课程名称并显示在页面 */
            Course byCno = couDao.findByCno(markInfo.getCourse().getCno());
            markInfo.setCourse(byCno);
            /** 替换上已批改的分数和id */
            if (markPro != null){
                markInfo.setId(markPro.getId());
                markInfo.setScore(markPro.getScore());
            }
        }
        model.addAttribute("markInfoPro",markInfoPro);
        return "tea/myMarkPro";
    }
    /** 课程成绩更改，成绩添加【Pro版】 */
    @PostMapping("addMarkPro")
    public String addMarkPro(Mark mark,String clzno,HttpSession session, Model model){
        System.out.println("成绩录入:\t"+mark);
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        if (mark.getId() > 0){
            int num = markDao.updateById(mark);
            System.out.println("更改成功：\t"+num);
        }else {
            mark.setTid(who.getUid());
            mark.setTpost(new Date());
            int num = markDao.addMark(mark);
            System.out.println("添加成功：\t"+num);
        }
        System.out.println("成绩录入\tclzno:\t"+clzno+"\tcno:\t"+mark.getCno());
        return "forward:/tea/findMarkPro";
    }
}
